﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;
using UniMana.Utils.Data;
using System.Runtime.InteropServices;
using ClosedXML.Excel;

namespace UniMana.Win.Training.xucUserControl.xucPersonel
{
    public partial class xucPersonelShedule : DevExpress.XtraEditors.XtraUserControl
    {

        private dynamic m_dnmSource;
        private DataTable m_dt;

      
        public xucPersonelShedule()
        {
            InitializeComponent();
        }


        public dynamic DataSource
        {
            get
            {
                return this.m_dnmSource;
            }
            set
            {
                this.xscPanel.Controls.Clear();
                this.m_dnmSource = value;
                if (value is IQueryable)
                {
                    this.m_dt = ((IQueryable)value).ToDataTable();
                }
                else
                {
                    this.m_dt = value;
                }
                if (m_dt != null)
                {
                    if (m_dt.Columns.Count == 9)
                    {
                        
                        DataTable dt = new DataTable();
                        for (int j = 0; j < this.m_dt.Columns.Count; j++)
                        {
                            dt.Columns.Add(this.m_dt.Columns[j].ColumnName);
                        }
                        for (int i = 0; i < m_dt.Rows.Count; i += 16)
                        {
                            dt.Rows.Clear();
                            for (int j = i; j < i+16; j++)
                            {
                                dt.Rows.Add(this.m_dt.Rows[j][0], this.m_dt.Rows[j][1], this.m_dt.Rows[j][2], this.m_dt.Rows[j][3], this.m_dt.Rows[j][4],
                                            this.m_dt.Rows[j][5],this.m_dt.Rows[j][6],this.m_dt.Rows[j][7],this.m_dt.Rows[j][8]);
                            }
                            xucWeek oneWeek = new xucWeek();
                            oneWeek.DataSource = dt;
                            if (xscPanel.Controls.Count > 0)
                            {
                                oneWeek.Top = xscPanel.Controls[xscPanel.Controls.Count - 1].Top + xscPanel.Controls[xscPanel.Controls.Count - 1].Height;
                            }
                            else
                            {
                                oneWeek.Top = 0;
                                oneWeek.Left = 0;
                            }
                            xscPanel.Controls.Add(oneWeek);
                            oneWeek.Show();
                        }
                    }
                }
            }
        }


        public void xPrint(string _strLastName,string _strSureName,string _strFromDay,string _strToDay)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Images", typeof(Image));
            for (int i = 0; i < this.xscPanel.Controls.Count; i++)
            {
                if (this.xscPanel.Controls[i].GetType().Name == "xucWeek")
                {
                    xucWeek week = this.xscPanel.Controls[i] as xucWeek;
                    dt.Rows.Add(week.Image);
                }
            }
            if (dt.Rows.Count > 0)
            {
                rptPersonelShedule report = new rptPersonelShedule(dt, "a", "b", "1-1-2012", "31-1-2012");
                report.ShowPreviewDialog();
            }
        }

        private void xscPanel_Click(object sender, EventArgs e)
        {
            xscPanel.Show();
        }

        public void ExportToXSLX(string _strPath = "Personel Shedule.xslx")
        {
            var workbook = new XLWorkbook();
            var worksheet = workbook.Worksheets.Add("UniMana");
            for (int k = 0; k < this.xscPanel.Controls.Count; k++)
            {
                if (this.xscPanel.Controls[k].GetType().Name == "xucWeek")
                {
                    xucWeek week = this.xscPanel.Controls[k] as xucWeek;
                    int row = k*16 + 1;
                    #region [Tạo khung]
                    worksheet.Range("C" + row + ":" + "F" + row).Merge();
                    worksheet.Cell("C" + row).Value = "Thứ 2";

                    worksheet.Range("G" + row + ":" + "J" + row).Merge();
                    worksheet.Cell("G" + row).Value = "Thứ 3";
                    worksheet.Range("K" + row + ":" + "N" + row).Merge();
                    worksheet.Cell("K" + row).Value = "Thứ 4";
                    worksheet.Range("O" + row + ":" + "R" + row).Merge();
                    worksheet.Cell("O" + row).Value = "Thứ 5";
                    worksheet.Range("S" + row + ":" + "V" + row).Merge();
                    worksheet.Cell("S" + row).Value = "Thứ 6";
                    worksheet.Range("W" + row + ":" + "Z" + row).Merge();
                    worksheet.Cell("W" + row).Value = "Thứ 7";
                    worksheet.Range("AA" + row + ":" + "AD" + row).Merge();
                    worksheet.Cell("AA" + row).Value = "Chủ nhật";
                    worksheet.Range("C" + row + ":" + "AD" + row).Style.Font.Bold = true;
                    worksheet.Range("C" + row + ":" + "AD" + row).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                    worksheet.Range("C" + row + ":" + "AD" + row).Style.Border.InsideBorder = XLBorderStyleValues.Thin;
                    worksheet.Range("A" + row + ":" + "AD" + (row + 15)).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                    worksheet.Range("B" + row + ":" + "AD" + row).Style.Fill.SetBackgroundColor(XLColor.FromArgb(214, 219, 255));
                    worksheet.Cell("A" + (row + 6)).Value = "Từ ngày";
                    worksheet.Cell("A" + (row + 6)).Style.Font.Bold = true;
                    worksheet.Cell("A" + (row + 7)).Value = week.FromDay;
                    worksheet.Cell("A" + (row + 9)).Value = "Từ ngày";
                    worksheet.Cell("A" + (row + 9)).Style.Font.Bold = true;
                    worksheet.Cell("A" + (row + 10)).Value = week.ToDay;
                    worksheet.Cell("B" + row).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;

                    worksheet.Cell("B" + (row + 1)).Value = "Ca: 1";
                    worksheet.Cell("B" + (row + 1)).Style.Font.Bold = true;
                    worksheet.Cell("B" + (row + 2)).Value = "Từ:";
                    worksheet.Cell("B" + (row + 2)).Style.Font.Bold = true;
                    worksheet.Cell("B" + (row + 4)).Value = "Đến:";
                    worksheet.Cell("B" + (row + 4)).Style.Font.Bold = true;


                    worksheet.Cell("B" + (row + 6)).Value = "Ca: 2";
                    worksheet.Cell("B" + (row + 6)).Style.Font.Bold = true;
                    worksheet.Cell("B" + (row + 7)).Value = "Từ:";
                    worksheet.Cell("B" + (row + 7)).Style.Font.Bold = true;
                    worksheet.Cell("B" + (row + 9)).Value = "Đến:";
                    worksheet.Cell("B" + (row + 9)).Style.Font.Bold = true;
                    worksheet.Cell("B" + (row + 11)).Value = "Ca: 3";
                    worksheet.Cell("B" + (row + 11)).Style.Font.Bold = true;
                    worksheet.Cell("B" + (row + 12)).Value = "Từ:";
                    worksheet.Cell("B" + (row + 12)).Style.Font.Bold = true;
                    worksheet.Cell("B" + (row + 14)).Value = "Đến:";
                    worksheet.Cell("B" + (row + 14)).Style.Font.Bold = true;
                    worksheet.Range("B" + (row + 1) + ":" + "B" + (row + 5)).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                    worksheet.Range("B" + (row + 6) + ":" + "B" + (row + 10)).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                    worksheet.Range("B" + (row + 11) + ":" + "B" + (row + 15)).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                    #endregion
                    #region [Thời gian của từng ca]
                    worksheet.Cell("B" + (row + 3)).Value = week.SessionFromTime[0];
                    worksheet.Cell("B" + (row + 5)).Value = week.SessionToTime[0];
                    worksheet.Cell("B" + (row + 8)).Value = week.SessionFromTime[1];
                    worksheet.Cell("B" + (row + 10)).Value = week.SessionToTime[1];
                    worksheet.Cell("B" + (row + 13)).Value = week.SessionFromTime[2];
                    worksheet.Cell("B" + (row + 15)).Value = week.SessionToTime[2];
                    #endregion
                    #region [Chèn dữ liệu từng ngày]
                    int start = 2;
                    int end = 26;
                    int count = 0;
                    for (int o = 0; o < 1; o++)
                    {
                        for (int i = 0; i < 3; i++)
                        {
                            for (int j = start; j <= end; j += 4)
                            {
                                int r = 0;
                                switch (i)
                                {
                                    case 0:
                                        r = row + 1;
                                        break;
                                    case 1:
                                        r = row + 6;
                                        break;
                                    case 2:
                                        r = row + 11;
                                        break;
                                }
                                string column1 = "";
                                string column2 = "";
                                string column3 = "";
                                string column4 = "";
                                switch (j)
                                {
                                    case 2:
                                        column1 = "C";
                                        column2 = "D";
                                        column3 = "E";
                                        column4 = "F";
                                        break;
                                    case 6:
                                        column1 = "G";
                                        column2 = "H";
                                        column3 = "I";
                                        column4 = "J";
                                        break;
                                    case 10:
                                        column1 = "K";
                                        column2 = "L";
                                        column3 = "M";
                                        column4 = "N";
                                        break;
                                    case 14:
                                        column1 = "O";
                                        column2 = "P";
                                        column3 = "Q";
                                        column4 = "R";
                                        break;
                                    case 18:
                                        column1 = "S";
                                        column2 = "T";
                                        column3 = "U";
                                        column4 = "V";
                                        break;
                                    case 22:
                                        column1 = "W";
                                        column2 = "X";
                                        column3 = "Y";
                                        column4 = "Z";
                                        break;
                                    case 26:
                                        column1 = "AA";
                                        column2 = "AB";
                                        column3 = "AC";
                                        column4 = "AD";
                                        break;
                                }
                                string border = column1 + r + ":" + column4 + (r + 4);
                                if (week.ClassID[count] != null)
                                {
                                    string classIDCell = column1 + r;
                                    worksheet.Cell(classIDCell).Value = "Mã lớp:";
                                    worksheet.Cell(classIDCell).Style.Font.Bold = true;
                                    string classIDValue = column2 + r + ":" + column4 + r;
                                    worksheet.Range(classIDValue).Merge();
                                    worksheet.Cell(column2 + r).Value = week.ClassID[count];

                                    string subjectNameCell = column1 + (r + 1) + ":" + column4 + (r + 3);
                                    worksheet.Range(subjectNameCell).Merge();
                                    worksheet.Range(subjectNameCell).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                                    worksheet.Range(subjectNameCell).Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                                    worksheet.Range(subjectNameCell).Style.Alignment.WrapText = true;
                                    worksheet.Cell(column1 + (r + 1)).Value = week.SubjectName[count];

                                    string creditsCell = column1 + (r + 4);
                                    worksheet.Cell(creditsCell).Value = "Số TC:";
                                    worksheet.Cell(creditsCell).Style.Font.Bold = true;
                                    string creditsCellValue = column2 + (r + 4);
                                    worksheet.Cell(creditsCellValue).Value = week.Credits[count];

                                    string roomCell = column3 + (r + 4);
                                    worksheet.Cell(roomCell).Value = "Phòng:";
                                    worksheet.Cell(roomCell).Style.Font.Bold = true;
                                    string roomCellValue = column4 + (r + 4);
                                    worksheet.Cell(roomCellValue).Value = week.Room[count];
                                }
                                else
                                {
                                    //worksheet.Range(border).Merge();
                                }
                                worksheet.Range(border).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                                count++;
                            }
                        }
                    }
                    #endregion
                }
            }
            workbook.SaveAs(_strPath);
        }
    }
}
